#!/usr/bin/env python
# coding: utf-8

# In[2]:


import numpy as np
import pandas as pd


# In[3]:


info = pd.read_csv('information.csv')
info.set_index(info['ID'],inplace=True)
#info


# In[4]:


goodID = pd.read_csv('goodID.csv')
#goodID['goodID']


# In[5]:


goodID_info = info.loc[goodID['goodID'],['ID', 'l', 'b', 'omega', 'radius', 'd0', 'ed0', 'deltad', 'edeltad',
                                         'meanebr', 'mass', 'sigma', 'v_near', 'v_far', 'W_co', 'N_hi', 
                                         'sigma_w_co', 'sigma_w_hi',
                                         'amplitude', 'mean', 'stddev',
                                         'sigma_amplitude', 'sigma_mean', 'sigma_stddev']]
#goodID_info
Xco = 2e+20
delta_Xco = Xco * 0.3


# In[20]:


raw_ncol = 10
raw_ncol_str = 'l'+'c'*(raw_ncol-1)
my_ncol = 9
my_ncol_str = 'l'+'c'*(my_ncol-1)

raw_header = 'ID & $l$ & $b$ & $\Omega$ & $r$ & $d_{0}$ & $\delta d$ & $\overline{E(B-V)}$ & $M$ & $\Sigma$ \\\\'
raw_unit = '& ($^\circ$) & ($^\circ$) & ($\mathrm{deg^2}$) & (pc) & (pc) & (pc) & (mag) & ($\mathrm{M_\odot}$) & ($\mathrm{M_\odot\cdot pc^{-2}}$) \\\\'

my_header = 'ID & $T_\mathrm{p}$ & $v_\mathrm{p}$ & $\sigma_{v}$ & $\Delta V$ & $W_\mathrm{CO}$ & $N(\mathrm{HI})$ & $N(\mathrm{H})$ &GDR \\\\'
my_unit = '& (K) & ($\mathrm{km\cdot s^{-1}}$) & ($\mathrm{km\cdot s^{-1}}$) & ($\mathrm{km\cdot s^{-1}}$) & ($\mathrm{K\cdot km\cdot s^{-1}}$) & ($\mathrm{10^{20}\, cm^{-2}}$) & ($\mathrm{10^{21}\, cm^{-2}}$) &($\mathrm{10^{21}\,cm^{-2}\cdot mag^{-1}}$)\\\\'


#数据
raw_data = ''
my_data = ''
for _, ID in enumerate(goodID_info['ID']):
    id_info = goodID_info.loc[ID]
    
    l = id_info['l']
    b = id_info['b']
    omega = id_info['omega']
    radius = id_info['radius']
    d0 = id_info['d0']
    ed0 = id_info['ed0']
    deltad = id_info['deltad']
    edeltad = id_info['edeltad']
    meanebr = id_info['meanebr']
    mass = id_info['mass']
    sigma = id_info['sigma']
    amplitude = id_info['amplitude']
    sigma_amplitude = id_info['sigma_amplitude']
    mean = id_info['mean']
    sigma_mean = id_info['sigma_mean']
    stddev = id_info['stddev']
    sigma_stddev = id_info['sigma_stddev']
    v_far = id_info['v_far']
    v_near = id_info['v_near']
    W_co = id_info['W_co']
    sigma_w_co = id_info['sigma_w_co']
    N_hi = id_info['N_hi']
    N_H = id_info['N_hi'] + 2*Xco*id_info['W_co']
    GDR = (id_info['N_hi']+2*Xco*id_info['W_co'])/2.33/id_info['meanebr']
    
    #计算误差
    delta_N_H = np.sqrt(
        (1.823e18*id_info['sigma_w_hi'])**2 + 
        (2*id_info['W_co']*delta_Xco)**2 +
        (2*Xco*id_info['sigma_w_co'])**2
    )

    delta_N_HI = np.sqrt(
        (1.823e18*id_info['sigma_w_hi'])**2
    )

    delta_N_H2 = np.sqrt(
        (id_info['W_co']*delta_Xco)**2 + 
        (Xco*id_info['sigma_w_co'])**2
    )
    
    delta_GDR = delta_N_H/2.33/id_info['meanebr']
    
    #写入文件
    raw_data_row = f'        {"$ %d $"%(ID)} & {"$ %.3f $"%(l)} & {"$ %.3f $"%(b)} & {"$ %.3f $"%(omega)} & {"$ %.1f $"%(radius)} & {"$ %.1f"%(d0)} \pm {"%.1f $"%(ed0)} & {"$ %.1f"%(deltad)} \pm {"%.1f $"%(edeltad)} & {"$ %.2f $"%(meanebr)} & {"$ %.1f $"%(mass)} & {"$ %.1f $"%(sigma)}\\\\\n'
    raw_data += raw_data_row
    
    my_data_row = f'        {"$ %d $"%(ID)} & {"$ %.2f"%(amplitude)} \pm {"%.2f $"%(sigma_amplitude)} & {"$ %.2f"%(mean)} \pm {"%.2f $"%(sigma_mean)} & {"$ %.2f"%(stddev)} \pm {"%.2f $"%(sigma_stddev)} & {"$ %.2f $"%(v_far-v_near)} & {"$ %.3f"%(W_co)} \pm {"%.3f $"%(sigma_w_co)} & {"$ %.3f"%(N_hi/1e20)} \pm {"%.3f $"%(delta_N_HI/1e20)} & {"$ %.3f"%(N_H/1e21)} \pm {"%.3f $"%(delta_N_H/1e21)} &{"$ %.3f"%(GDR/1e21)} \pm {"%.3f $"%(delta_GDR/1e21)}\\\\\n'
    my_data += my_data_row

#写文件
with open('raw_tab_latex.tex','w+',encoding='utf-8') as tab_latex:
    tab_latex.write(
        f'\\begin{{longtable}}{{{raw_ncol_str}}}\n'
        f'    \\caption{{An example table.}}\n'
        f'    \\label{{tab:raw_example}}\\\\\n'
        f'\n'
        f'        \\toprule\n'
        f'        {raw_header}\n'
        f'        {raw_unit}\n'
        f'        \\midrule\n'
        f'    \\endfirsthead\n'
        f'\n'
        f'        \\multicolumn{{{raw_ncol}}}{{r}}{{(接上页)}}\\\\ \n'
        f'        \\multicolumn{{{raw_ncol}}}{{c}}{{续\\autoref{{tab:raw_example}}}}\\\\ \n'
        f'        \\toprule\n'
        f'        {raw_header}\n'
        f'        {raw_unit}\n'
        f'        \\midrule\n'
        f'    \\endhead\n'
        f'\n'
        f'        \\bottomrule\n'
        f'        \\multicolumn{{{raw_ncol}}}{{r}}{{(接下页)}}\\\\ \n'
        f'    \\endfoot\n'
        f'\n'
        f'        \\bottomrule\n'
        f'    \\endlastfoot\n'
        f'\n'
        f'{raw_data}'
        f'\\end{{longtable}}\n'
    )

with open('my_tab_latex.tex','w+',encoding='utf-8') as tab_latex:
    tab_latex.write(
        f'\\begin{{longtable}}{{{my_ncol_str}}}\n'
        f'    \\caption{{An example table.}}\n'
        f'    \\label{{tab:my_example}}\\\\\n'
        f'\n'
        f'        \\toprule\n'
        f'        {my_header}\n'
        f'        {my_unit}\n'
        f'        \\midrule\n'
        f'    \\endfirsthead\n'
        f'\n'
        f'        \\multicolumn{{{my_ncol}}}{{r}}{{(接上页)}}\\\\ \n'
        f'        \\multicolumn{{{my_ncol}}}{{c}}{{续\\autoref{{tab:my_example}}}}\\\\ \n'
        f'        \\toprule\n'
        f'        {my_header}\n'
        f'        {my_unit}\n'
        f'        \\midrule\n'
        f'    \\endhead\n'
        f'\n'
        f'        \\bottomrule\n'
        f'        \\multicolumn{{{my_ncol}}}{{r}}{{(接下页)}}\\\\ \n'
        f'    \\endfoot\n'
        f'\n'
        f'        \\bottomrule\n'
        f'    \\endlastfoot\n'
        f'\n'
        f'{my_data}'
        f'\\end{{longtable}}\n'
    )


# In[ ]:




